package com.shawn.engine;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shawn on 2016/4/8.
 */
public class ContactEngine {


    /**
     * 获取系统联系人
     * @return
     */
    public static List<Map<String, String>> getAllContactInfo(Context context) {

        List<Map<String, String>> list = new ArrayList<>();
        // 1.获取内容解析者
        ContentResolver resolver = context.getContentResolver();
        String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
        Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
        while (cursor.moveToNext()) {
            Map<String, String> map = new HashMap<>();
            String name = cursor.getString(0);
            String number = cursor.getString(1);
            map.put("name", name);
            map.put("phone", number);
            list.add(map);
        }
        cursor.close();
        return list;
    }
}
